<?php

namespace App\Services\Com;

use function GuzzleHttp\Psr7\str;

class IdCardService
{

    private static function idcard_verify_number($idcard_base)
    {
        if (strlen($idcard_base) != 17) {
            return false;
        }
        //加权因子
        $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
        //校验码对应值
        $verify_number_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
        $checksum = 0;
        for ($i = 0; $i < strlen($idcard_base); $i++) {
            $checksum += substr($idcard_base, $i, 1) * $factor[$i];
        }
        $mod = $checksum % 11;
        $verify_number = $verify_number_list[$mod];
        return $verify_number;
    }

    /**
     * 将15位身份证升级到18位
     * @param string $idcard
     * @return boolean
     */
    private static function idcard_15to18($idcard)
    {
        if (strlen($idcard) != 15) {
            return false;
        } else {
            // 如果身份证顺序码是996 997 998 999，这些是为百岁以上老人的特殊编码
            if (array_search(substr($idcard, 12, 3), array('996', '997', '998', '999')) !== false) {
                $idcard = substr($idcard, 0, 6) . '18' . substr($idcard, 6, 9);
            } else {
                $idcard = substr($idcard, 0, 6) . '19' . substr($idcard, 6, 9);
            }
        }
        $idcard = $idcard . self::idcard_verify_number($idcard);
        return $idcard;
    }

    /**
     * 18位身份证校验码有效性检查
     * @param string $idcard
     * @return boolean|string
     */
    public static function check($idcard)
    {
        if (strlen($idcard) == 15) {
            $idcard = self::idcard_15to18($idcard);
        }
        if (strlen($idcard) != 18) {
            return false;
        }
        $idcard_base = substr($idcard, 0, 17);
        if (self::idcard_verify_number($idcard_base) != strtoupper(substr($idcard, 17, 1))) {
            return false;
        } else {
            return $idcard;
        }
    }

    /**
     * 验证其他身份证号，港澳台身份证
     * @param $IDCard
     * @return bool
     */
    public static function checkOtherIDCard($IDCard)
    {
        $IDCard = strtoupper($IDCard);
        $IDCard = str_replace(array('（', '）'), array('(', ')'), $IDCard);
        preg_match('/^([A-Z])([\d]{6})\(([A\d])\)$/', $IDCard, $hongkong);//香港
        if ($hongkong && count($hongkong) === 4) {
            $sum = (ord($hongkong[1]) - 64) * 8;
            $index = 7;
            for ($j = 0; $j < 6; $j++) {
                $sum += $hongkong[2][$j] * $index;
                $index--;
            }
            $get_num = $sum % 11;
            if ($get_num === 1) {
                $get_num = 'A';
            } elseif ($get_num > 1) {
                $get_num = 11 - $get_num;
            }

            if ($hongkong[3] == $get_num) {
                return true;
            }
            return false;
        }
        preg_match('/^([A-Z])([\d]{9})$/', $IDCard, $taiwan);//中国台湾省
        if ($taiwan && count($taiwan) === 3)//首位数字代表性别，男性为1、女性为2
        {
            $area_code = array('A' => 10, 'B' => 11, 'C' => 12, 'D' => 13, 'E' => 14, 'F' => 15, 'G' => 16, 'H' => 17, 'I' => 34, 'J' => 18, 'K' => 19, 'L' => 20, 'M' => 21, 'N' => 22, 'O' => 35, 'P' => 23, 'Q' => 24, 'R' => 25, 'S' => 26, 'T' => 27, 'U' => 28, 'V' => 29, 'W' => 32, 'X' => 30, 'Y' => 31, 'Z' => 33);
            $code = $area_code[$taiwan[1]];

            $code = ''.$code;
            $sum = $code[0] + $code[1] * 9;

            $index = 8;

            for ($k = 1; $k < 8; $k++) {
                $sum += $taiwan[2][$k] * $index;
                $index--;
            }

            $get_num = $sum % 10;
            if ($get_num == $taiwan[2][8]) {
                return true;
            }
            return false;
        }
        preg_match('/^[157][\d]{6}\([\d]\)$/', $IDCard, $aomen);//澳门
        if ($aomen) {
            return true;
        }
        return false;
    }

    //验证其他证件
    public static function checkOther($IDCard){
        //港澳通行证验证
        if(preg_match('/^[HMhm]{1}([0-9]{10}|[0-9]{8})$/', $IDCard)){
            return true;
        }

        ////台胞证验证
        if(preg_match('/(^\d{8})$/', $IDCard)){
            return true;
        }

        ////回乡证
        if(preg_match('/(H|M)(\d{10})$/', $IDCard)){
            return true;
        }

        //台湾通行证 /[A-Z][0-9]{9}/
        if(preg_match('/^[a-zA-Z][0-9]{9}$/', $IDCard)){
            return true;
        }

        //护照验证
        if(preg_match('/^1[45][0-9]{7}$|([P|p|S|s]\d{7}$)|([S|s|G|g]\d{8}$)|([Gg|Tt|Ss|Ll|Qq|Dd|Aa|Ff]\d{8}$)|([H|h|M|m]\d{8,10})$/', $IDCard)){
            return true;
        }

        return false;
    }
}
